package olympicgamesgui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import olympicgameslogic.*;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author Nuno
 */
public class DialogManageEditionEvents extends JDialog {

   private JTable eventsTable;
   private JScrollPane scroller;
   private DefaultTableModel TableModel;
   Modality mod;
   Discipline disc;
   DisciplineEvent discEv;
   private JComboBox modalityCombo;
   private JComboBox disciplineCombo;
   private JComboBox eventCombo;
   private ImageIcon img;
   private String tit;
   private int selectedLine;
   String[] columnNames = {"Name", "Modality", "Discipline", "Gender"};
   ClassLoader cldr = this.getClass().getClassLoader();
   LinkedList<Modality> availableModalityes = OlympicGamesGui.ap.getModalities();
   LinkedList<Discipline> disciplines;
   LinkedList<DisciplineEvent> disciplineEvents;
   LinkedList<EditionDisciplineEvent> edDiscEvts;

   public DialogManageEditionEvents(Frame father, String title, Edition edition) {
      super(father, title, true);
      super.setIconImage(Toolkit.getDefaultToolkit().getImage(cldr.getResource("images/extra/icon.png")));
      setLayout(new BorderLayout());
      setBackground(Color.white);

      edDiscEvts = OlympicGamesGui.ap.getEdition(edition.getYear()).getEditionDisciplineEventList();


      JPanel panNorth = new JPanel(new FlowLayout(FlowLayout.CENTER));
      panNorth.setSize(200, 60);
      panNorth.setBackground(Color.white);
      JLabel background = new JLabel(title);
      background.setFont(new Font("Arial", Font.BOLD, 14));
      background.setIcon(img);
      panNorth.add(background);


      JPanel panCenter = new JPanel();
      panCenter.setLayout(new BorderLayout(10, 10));
      panCenter.setBackground(Color.white);



      JPanel pCenterNorth = new JPanel(new GridLayout(4, 2, 10, 10));
      pCenterNorth.setBackground(Color.WHITE);
      JLabel modLabel = new JLabel("Modality");
      pCenterNorth.add(modLabel);

      modalityCombo = new JComboBox(availableModalityes.toArray());
      modalityCombo.setSelectedIndex(-1);
      modalityCombo.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    JComboBox box = (JComboBox) e.getSource();
	    if (box.getSelectedIndex() > -1) {
	       mod = availableModalityes.getElement(modalityCombo.getSelectedIndex());
	       disciplines = mod.getDisciplines();
	       disciplineCombo.setModel(new DefaultComboBoxModel(disciplines.toArray()));
	       disciplineCombo.setSelectedIndex(-1);
	       disciplineCombo.setEnabled(true);
	    }

	 }
      });
      pCenterNorth.add(modalityCombo);


      JLabel discLabel = new JLabel("Discipline");
      pCenterNorth.add(discLabel);

      disciplineCombo = new JComboBox();
      disciplineCombo.setEnabled(false);
      disciplineCombo.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    JComboBox box = (JComboBox) e.getSource();
	    disc = disciplines.getElement(box.getSelectedIndex());
	    disciplineEvents = disc.getEventsList();
	    LinkedList<DisciplineEvent> temp_list = new LinkedList<>();

	    for (DisciplineEvent disEv : disciplineEvents) {
	       for (EditionDisciplineEvent edisev : edDiscEvts) {
		  DisciplineEvent dev = edisev.getEditionDisciplineEvent();
		  if (dev.equals(disEv) &&temp_list.contains(disEv) ) {
		     
		     temp_list.add(disEv);
		  }
	       }

	    }
	    System.out.println(temp_list.size());
	    eventCombo.setModel(
		    new DefaultComboBoxModel(temp_list.toArray()));
	    eventCombo.setSelectedIndex(
		    -1);
	    eventCombo.setEnabled(
		    true);

	 }
      });



      pCenterNorth.add(disciplineCombo);


      JLabel eventLabel = new JLabel("Dciscipline Event");
      pCenterNorth.add(eventLabel);
      eventCombo = new JComboBox();
      eventCombo.setEnabled(false);

      eventCombo.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	 }
      });
      pCenterNorth.add(eventCombo);



      eventsTable = new JTable() {

	 public boolean isCellEditable(int rowIndex, int colIndex) {
	    return false; //Disallow the editing of any cell
	 }
      };
      TableModel = (DefaultTableModel) eventsTable.getModel();
      TableModel.setColumnIdentifiers(columnNames);
      setUpTableData();

      JTableHeader header = eventsTable.getTableHeader();
      header.setBackground(Color.DARK_GRAY);
      header.setFont(new Font("Arial", Font.BOLD, 12));
      header.setForeground(Color.WHITE);
      eventsTable.setPreferredScrollableViewportSize(new Dimension(300, 70));
      eventsTable.setFillsViewportHeight(true);
      eventsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      eventsTable.getSelectionModel().addListSelectionListener(new DialogManageEditionEvents.RowListener());
      eventsTable.getTableHeader().setReorderingAllowed(false);
      scroller = new JScrollPane(eventsTable);
      panCenter.add(pCenterNorth, BorderLayout.NORTH);

      panCenter.add(scroller, BorderLayout.CENTER);
//painel com botoes        
      JPanel pansul = new JPanel();
      pansul.setSize(200, 20);
      pansul.setBackground(Color.white);
      JButton btok = new JButton("OK", new ImageIcon(cldr.getResource("images/extra/btok.png")));
      pansul.setLayout(new FlowLayout(FlowLayout.CENTER));
      pansul.add(btok);


      add(panNorth, BorderLayout.NORTH);
      add(panCenter, BorderLayout.CENTER);
      add(pansul, BorderLayout.SOUTH);
      pack();
      setLocationRelativeTo(null);

      setDefaultCloseOperation(DISPOSE_ON_CLOSE);



      btok.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	 }
      });






   }

   public void showJan() {
      setVisible(true);





   }

   private class RowListener implements ListSelectionListener {

      @Override
      public void valueChanged(ListSelectionEvent event) {
//	 if (event.getValueIsAdjusting()) {
//	    return;
//	 }
//	 selectedLine = eventsTable.getSelectedRow();
//	 if (selectedLine == -1) {
//	    selectedLine = 0;
//	 } else {
//	    for (Discipline dis : mod.getDisciplines()) {
//	       if (eventsTable.getValueAt(selectedLine, 0).toString().equals(dis.getName())) {
//	//	  dis_temp = dis;
//	       }
//	    }
//	 }
      }
   }

   private void setUpTableData() {
      Object[][] data1 = new Object[edDiscEvts.getSize()][4];
      int i = 0;

      for (EditionDisciplineEvent ev : edDiscEvts) {
	 data1[i][0] = ev.getEditionDisciplineEvent().getEventName();
	 data1[i][1] = ev.getModality().getName();
	 data1[i][2] = ev.getDiscipline().getName();
	 data1[i][3] = ev.getEditionDisciplineEvent().getEventGender();


	 TableModel.addRow(data1[i]);
	 i++;
      }
      eventsTable.setModel(TableModel);
   }

   public void refreshTable() {
      TableModel.setRowCount(0);
      eventsTable.removeAll();
      setUpTableData();
   }
}
